package com.film.service.impl;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.film.dao.ReviewDao;
import com.film.dao.impl.ReviewDaoImpl;
import com.film.domain.Film;
import com.film.domain.Review;
import com.film.domain.ReviewAndFilm;
import com.film.domain.User;
import com.film.service.FilmService;
import com.film.service.ReviewService;

public class ReviewServiceImpl implements ReviewService {
	private ReviewDao reviewDao = new ReviewDaoImpl();
	private FilmService filmService = new FilmServiceImpl();
	@Override
	public int insertReview(int uId, int filmId, String content, Float revScore) {
		Date date = new Date();
		DateFormat datefoemate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = datefoemate.format(date);
		Film film = new Film(filmId);
		User user = new User(uId);
		Review review = new Review(film,user,time,content,revScore,0);
		int result = reviewDao.insertReview(review);
		//添加完成后修改电影的评分
		//先获取评分表里面的全部数据
		ReviewAndFilm reviewAndFilm = reviewDao.selectFilmReviewScore(filmId);
		//修改电影的平均分数
		float sum = reviewAndFilm.getRevieScorewSum();//总分数
		int count = reviewAndFilm.getReviewCount();//人数
		DecimalFormat de = new DecimalFormat("##0.0");
		String reviewScoreint = de.format(sum/count);
		filmService.updateFilmReviewScore(reviewScoreint, filmId);
		return result;
	}

	@Override
	public List<Review> selectAllReview(int filmId) {
		
		return reviewDao.selectAllReview(filmId);
	}
	
	//该方法用来做评论的查重
	@Override
	public Review selectReviewInfo(int filmId, int uId) {
		
		return reviewDao.selectReviewInfo(filmId, uId);
	}

	@Override
	public int updateReview(int uId,int filmId,String content,Float revScore) {
		Date date = new Date();
		DateFormat datefoemate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = datefoemate.format(date);
		Film film = new Film(filmId);
		User user = new User(uId);
		Review review = new Review(film,user,time,content,revScore,0);
		int result = reviewDao.updateReview(review);
		//添加完成后修改电影的评分
		//先获取评分表里面的全部数据
		ReviewAndFilm reviewAndFilm = reviewDao.selectFilmReviewScore(filmId);
		//修改电影的平均分数
		float sum = reviewAndFilm.getRevieScorewSum();//总分数
		int count = reviewAndFilm.getReviewCount();//人数
		DecimalFormat de = new DecimalFormat("##0.0");
		String reviewScoreint = de.format(sum/count);
		filmService.updateFilmReviewScore(reviewScoreint, filmId);
		return result;
	}

	@Override
	public int updateReviewPraise(int praise, int revId) {
		// TODO Auto-generated method stub
		return reviewDao.updateReviewPraise(praise, revId);
	}

	@Override
	public ReviewAndFilm selectFilmReviewScore(int film_id) {
		return reviewDao.selectFilmReviewScore(film_id);
	}

	@Override
	public List<Review> selectReview() {
		return reviewDao.selectReview();
	}
	@Override
	public int deleteReviewById(int rev_id) {
		
		return reviewDao.deleteReviewById(rev_id);
	}
	
	@Override
	public List<Review> selectlimit(int a) {
		int stripNum=7;//默认分页的查询条数为7条
		int pageNums=0;
		if(a==0) {
			 pageNums=0;
		}else {
			 pageNums=(a-1)*stripNum;
		}
		
		return reviewDao.selectlimit(pageNums, stripNum);
	}

	@Override
	public int deleteReviewPraise(int reviewId) {
		// TODO Auto-generated method stub
		return reviewDao.deleteReviewPraise(reviewId);
	}

	@Override
	public int updateReviewScore(int revId) {
		//根据评论id查询电影
		int filmId = reviewDao.selectFilmIdByRevId(revId);
		int result1 = deleteReviewById(revId);
		ReviewAndFilm rf = reviewDao.selectFilmReviewScore(filmId);
		if(rf != null) {
			float sum = rf.getRevieScorewSum();//总分数
			int count = rf.getReviewCount();//人数
			DecimalFormat de = new DecimalFormat("##0.0");
			String reviewScoreint = de.format(sum/count);
			return filmService.updateFilmReviewScore(reviewScoreint, filmId);
		}
		return filmService.updateFilmReviewScore(null, filmId);
	}
	
	

}
